﻿namespace Home.Mvc
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Web;
    using System.Web.Hosting;
    using Home.Services.ProductService;
    using Home.Services.SaleService;
    using Home.Services.SecurityService;
    using Home.Services.SsisService;
    using Home.Services.UtilityService;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    public static class BusinessHelper
    {
        private static readonly Lazy<JObject> JsonLazy = new Lazy<JObject>(() =>
        {
            var path = HostingEnvironment.MapPath("~/app_data/business.json");
            using (var streamReader = new StreamReader(path, Encoding.UTF8))
            {
                using (var jsonReader = new JsonTextReader(streamReader))
                {
                    return JObject.Load(jsonReader);
                }
            }
        }, LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> ProductsLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("Products"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> ContactMethodsLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("ContactMethods"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> DiamondTypesLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("DiamondTypes"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondCutsLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondCuts"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondClaritiesLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondClarities"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondColorsLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondColors"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondPolishesLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondPolishes"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondReportsLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondReports"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondShapesLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondShapes"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyList<string>> DiamondSymmetriesLazy = new Lazy<IReadOnlyList<string>>(() => GetLazyList("DiamondSymmetries"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> ProductStatusesLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("ProductStatuses"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> ProductTypesLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("ProductTypes"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> ProductUploadTypesLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("ProductUploadTypes"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> ProductUploadStatusesLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("ProductUploadStatuses"), LazyThreadSafetyMode.PublicationOnly);
        private static readonly Lazy<IReadOnlyDictionary<string, string>> ImportTypesLazy = new Lazy<IReadOnlyDictionary<string, string>>(() => GetLazyDictionary("ImportTypes"), LazyThreadSafetyMode.PublicationOnly);

        public static JObject BusinessJson
        {
            get { return JsonLazy.Value; }
        }

        public static IReadOnlyList<string> Products
        {
            get { return ProductsLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> ContactMethods
        {
            get { return ContactMethodsLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> DiamondTypes
        {
            get { return DiamondTypesLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondCuts
        {
            get { return DiamondCutsLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondClarities
        {
            get { return DiamondClaritiesLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondColors
        {
            get { return DiamondColorsLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondPolishes
        {
            get { return DiamondPolishesLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondReports
        {
            get { return DiamondReportsLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondShapes
        {
            get { return DiamondShapesLazy.Value; }
        }

        public static IReadOnlyList<string> DiamondSymmetries
        {
            get { return DiamondSymmetriesLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> ProductStatuses
        {
            get { return ProductStatusesLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> ProductTypes
        {
            get { return ProductTypesLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> ProductUploadTypes
        {
            get { return ProductUploadTypesLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> ProductUploadStatuses
        {
            get { return ProductUploadStatusesLazy.Value; }
        }

        public static IReadOnlyDictionary<string, string> ImportTypes
        {
            get { return ImportTypesLazy.Value; }
        }

        public static IReadOnlyList<User> Employees
        {
            get
            {
                using (var db = new SecurityDbContext())
                {
                    return db.Users.ToArray();
                }
            }
        }

        private static IReadOnlyList<string> GetLazyList(string name)
        {
            var result = new List<string>();

            var array = (JArray)BusinessJson[name];
            foreach (var item in array)
            {
                result.Add((string)item);
            }

            return result;
        }

        private static IReadOnlyDictionary<string, string> GetLazyDictionary(string name)
        {
            var result = new Dictionary<string, string>();

            var array = (JArray)BusinessJson[name];
            foreach (var item in array)
            {
                result.Add((string)item["value"], (string)item["text"]);
            }

            return result;
        }
    }
}